package com.whfc.hdc.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.whfc.common.exception.BizException;
import com.whfc.common.result.PageData;
import com.whfc.common.result.ResultEnum;
import com.whfc.common.util.PageUtil;
import com.whfc.hdc.dao.AppDeviceMapper;
import com.whfc.hdc.dao.FshDeviceMediaMapper;
import com.whfc.hdc.dto.FshDeviceMediaDTO;
import com.whfc.hdc.entity.AppDevice;
import com.whfc.hdc.entity.FshDeviceMedia;
import com.whfc.hdc.params.FshDeviceMediaParam;
import com.whfc.hdc.service.FshDeviceMediaService;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

/**
 * @author qzexing
 * @version 1.0
 * @date 2021-04-08 17:59
 */
@DubboService(interfaceClass = FshDeviceMediaService.class, version = "1.0.0")
public class FshDeviceMediaServiceImpl implements FshDeviceMediaService {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private FshDeviceMediaMapper fshDeviceMediaMapper;

    @Autowired
    private AppDeviceMapper appDeviceMapper;

    @Override
    public PageData<FshDeviceMediaDTO> list(String sn, Integer pageNum, Integer pageSize, Date startTime,
                                            Date endTime, List<Integer> mediaTypes, List<Integer> sources) throws BizException {
        logger.info("获取视频安全帽多媒体信息, sn:{}, pageNum:{}, pageSize:{}, startTime:{}, endTime:{}, mediaTypes:{}," +
                " sources:{}", sn, pageNum, pageSize, startTime, endTime, mediaTypes, sources);
        PageHelper.startPage(pageNum, pageSize);
        List<FshDeviceMediaDTO> list = fshDeviceMediaMapper.selectMediaList(sn, startTime, endTime, mediaTypes, sources);
        return PageUtil.pageData(PageInfo.of(list));
    }

    @Override
    public void addDeviceMedia(FshDeviceMediaParam params) throws BizException {
        logger.info("保存设备文件, params:{}", params);
        AppDevice appDevice = appDeviceMapper.selectBySn(params.getSn());
        if (appDevice == null) {
            throw new BizException(ResultEnum.PARAM_ERROR.getCode(), "设备不存在");
        }
        FshDeviceMedia fshDeviceMedia = new FshDeviceMedia();
        BeanUtils.copyProperties(params, fshDeviceMedia);
        fshDeviceMedia.setDeviceId(appDevice.getId());
        fshDeviceMediaMapper.insertSelective(fshDeviceMedia);
    }
}
